package Controller;

import javafx.fxml.FXML;
import javafx.event.ActionEvent;
import javafx.scene.control.*;
import javafx.stage.Stage;

import javax.lang.model.element.VariableElement;
import java.util.Optional;


/**
 * @program: StudentMS
 * @description: login stage
 * @author: XuYuanjun
 * @create: 2020-03-07 12:48
 **/
public class LoginStageController {

    private Stage _stage;

    public void setStage(Stage stage) {
        _stage = stage;
    }

    @FXML
    private TextField userNameTextField;

    @FXML
    private Button cancelButton;

    @FXML
    private PasswordField pwdField;

    @FXML
    private Button loginBButton;

    @FXML
    public void loginButtonEvent(ActionEvent event) {
        Alert alert = null;
        boolean res = login(userNameTextField.getText(), pwdField.getText());
        if (res) {
            Optional<ButtonType> buttonType = Utils.Inst.setToolTipMsg(Alert.AlertType.INFORMATION,"登录成功");
            if (buttonType.get().getButtonData() == ButtonBar.ButtonData.YES){
                // todo :goto main stage
            }
            return;
        }
        Utils.Inst.setErrorMsg(Alert.AlertType.ERROR,"登录失败：用户名或密码错误");
    }

    @FXML
    public void cancelButtonEvent(ActionEvent event) {
        _stage.close();
    }

    private Boolean login(String userName, String pwd) {
        if (! userName.isEmpty() && ! pwd.isEmpty()) {
            return true;
        }
        return false;
    }

}

